package com.bailan.controller;

import com.bailan.pojo.DAO.FillingInformation;
import com.bailan.pojo.DTO.YiQinShuJuDTO;
import com.bailan.pojo.Student;
import com.bailan.pojo.Yiqinshuju;
import com.bailan.service.IStudentService;
import com.bailan.service.IYiqinshujuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiLan
 * @since 2022:05:24
 */
@RestController
@RequestMapping("/yiqinshuju")
public class YiqinshujuController {

    private IYiqinshujuService yiqinshujuService;
    private IStudentService studentService;

    @Autowired
    public void setStudentService(IStudentService studentService) {
        this.studentService = studentService;
    }

    @Autowired
    public void setYiqinshujuService(IYiqinshujuService yiqinshujuService) {
        this.yiqinshujuService = yiqinshujuService;
    }

    @GetMapping
    @ResponseBody
    public YiQinShuJuDTO getData(){

        YiQinShuJuDTO yiQinShuJuDTO = new YiQinShuJuDTO();

        List<FillingInformation> informations= new ArrayList<>();

        List<Yiqinshuju> list = yiqinshujuService.list(null);
        List<Student> students = studentService.list(null);

        LocalDate now = LocalDate.now();


        //学生人数
        long count = students.size();
        yiQinShuJuDTO.setNums(count);

        //健康人数
        long todayHealth = list.stream()
                .filter(yiqinshuju -> yiqinshuju.getDDate().isEqual(now))
                .filter(yiqinshuju -> yiqinshuju.getDIsFever() == 0)
                .count();
        yiQinShuJuDTO.setToday_health(todayHealth);

        //生病人数
        long illnessNums = list.stream()
                .filter(yiqinshuju -> yiqinshuju.getDIsFever().equals(1))
                .count();
        yiQinShuJuDTO.setToday_illness(illnessNums);

        //今日填报人数
        long todayNums = list.stream()
                .filter(yiqinshuju -> yiqinshuju.getDDate().isEqual(now))
                .count();
        yiQinShuJuDTO.setToday_nums(todayNums);


        //今日未填报数量
        yiQinShuJuDTO.setToday_no_nums(count-todayNums);



        LocalDate localDate = now.minusDays(1);

        for (int i=1;i<6;i++){
            LocalDate date = now.minusDays(i);
            long fillNum = list.stream()
                    .filter(yiqinshuju -> yiqinshuju.getDDate().isEqual(date))
                    .count();

            FillingInformation fillingInformation = new FillingInformation();
            fillingInformation.setDate(date);
            fillingInformation.setFillNumber(fillNum);
            fillingInformation.setNotFillNumber(count-fillNum);

            informations.add(fillingInformation);
        }


        //过去填报信息
        yiQinShuJuDTO.setFillingInformation(informations);



        return yiQinShuJuDTO;
    }

    @PostMapping
    @ResponseBody
    public boolean fillInfo(@RequestBody Yiqinshuju yiqinshuju){

        System.out.println(yiqinshuju);

        LambdaQueryWrapper<Yiqinshuju> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Yiqinshuju::getDDate,yiqinshuju.getDDate());
        queryWrapper.in(Yiqinshuju::getSId,yiqinshuju.getSId());
        return yiqinshujuService.updateOrSave(yiqinshuju);
    }

}
